/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import POJOS.Song;
import POJOS.Playlist;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import GUI.PlayListForm;
/**
 *
 * @author john
 */
public class SongsList extends javax.swing.JFrame {
    int selectedRow;
    Song song;
    SongsList songsList;

    /**
     * Creates new form SongsList
     */
    public SongsList() {
        initComponents();
        checkButtons();
        
        songsList_tl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
       
        for(Song s: NewOrModifyPlayListForm.songList){
            songList.remove(s);
        }
    }
    

    public SongsList(Playlist playList) {
        initComponents();
        checkButtons();
        songsList_tl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        
        RadioStationPUEntityManager.getTransaction().begin();
        playList = RadioStationPUEntityManager.merge(playList);
        RadioStationPUEntityManager.getTransaction().commit();
        
       
        for (Song s : playList.getSongCollection()){
            songList.remove(s);    
        }
        
        for(Song s: NewOrModifyPlayListForm.songList){
            songList.remove(s);
        }
        
        for(Song s:  playList.getSongCollection()){
            if(!NewOrModifyPlayListForm.songList.contains(s)){
                songList.add(s);
            }
        }
    }
    
    private void checkButtons(){
        selectedRow = songsList_tl.getSelectedRow();
        inputSongInList_btn.setEnabled(selectedRow>=0);
        
        if (selectedRow>=0){
            song = songList.get(songsList_tl.convertRowIndexToModel(selectedRow));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationPU").createEntityManager();
        songQuery = java.beans.Beans.isDesignTime() ? null : RadioStationPUEntityManager.createQuery("SELECT s FROM Song s");
        songList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : songQuery.getResultList();
        jScrollPane1 = new javax.swing.JScrollPane();
        songsList_tl = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        exitSongslist_btn = new javax.swing.JButton();
        inputSongInList_btn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, songList, songsList_tl);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("Title");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idAlbum.idArtist.artisticName}"));
        columnBinding.setColumnName("Artist");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idAlbum.idMusicGroup.nameMusicGroup}"));
        columnBinding.setColumnName("Music Group");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${duration}"));
        columnBinding.setColumnName("Duration");
        columnBinding.setColumnClass(Float.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        songsList_tl.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                songsList_tlMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(songsList_tl);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Λίστα Τραγουδιών");

        exitSongslist_btn.setText("ΕΞΟΔΟΣ");
        exitSongslist_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitSongslist_btnActionPerformed(evt);
            }
        });

        inputSongInList_btn.setText("ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΛΙΣΤΑ");
        inputSongInList_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inputSongInList_btnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(361, 361, 361)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(143, 143, 143)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(inputSongInList_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(333, 333, 333)
                                .addComponent(exitSongslist_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 633, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel1)
                .addGap(31, 31, 31)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(inputSongInList_btn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(exitSongslist_btn, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE))
                .addContainerGap(62, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitSongslist_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitSongslist_btnActionPerformed
        dispose();
    }//GEN-LAST:event_exitSongslist_btnActionPerformed

    private void inputSongInList_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputSongInList_btnActionPerformed
        if (!NewOrModifyPlayListForm.songList.contains(song)) {
            NewOrModifyPlayListForm.songList.add(NewOrModifyPlayListForm.songList.size(), song);
            songList.remove(song);
        }else{
           JOptionPane.showMessageDialog(null, "Το τραγούδι υπάρχει ήδη!", "ERROR", JOptionPane.ERROR_MESSAGE);
       }
     
        checkButtons();
    }//GEN-LAST:event_inputSongInList_btnActionPerformed

    private void songsList_tlMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_songsList_tlMouseClicked
        checkButtons();
    }//GEN-LAST:event_songsList_tlMouseClicked
    
    /*private void songsList_tlMouseClicked(java.awt.event.MouseEvent evt) {                                          
        
        checkButtons();
    }*/
      
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SongsList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SongsList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SongsList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SongsList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SongsList().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager RadioStationPUEntityManager;
    private javax.swing.JButton exitSongslist_btn;
    private javax.swing.JButton inputSongInList_btn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private java.util.List<POJOS.Song> songList;
    private javax.persistence.Query songQuery;
    private javax.swing.JTable songsList_tl;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    /*private void checkButtons() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }*/
}
